package com.yashi.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageInfo;
import com.yashi.entity.TbDepartment;
import com.yashi.entity.TbUser;
import com.yashi.service.DepartmentService;
import com.yashi.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Resource
	private UserService userService;
	
	@Resource
	private DepartmentService departmentService;
	
	/**
	 * 测试
	 * @return
	 */
	@RequestMapping("/gologin")
	public String goLogin(){
		
		return "login";
	}

	/**
	 * web登录验证
	 * @param user
	 * @param session
	 * @return
	 */
	@RequestMapping("/login")
	public String userLogin(TbUser user,HttpSession session,HttpServletRequest request){
		TbUser user1=null;
		try {
			user1 = userService.selectByUser1(user);
			session.setAttribute("user", user1);
			session.setMaxInactiveInterval(-1);
			return "adminMain";
		} catch (Exception e) {
			request.setAttribute("error", e.getMessage());
			return "login";
		}
		
	}
	
	/**
	 * 跳转至添加用户页面
	 * @return
	 */
	@RequestMapping("/toAddUser")
	public String toAddUser(HttpServletRequest request){
		
		try {
			List<TbDepartment> findDeptList = departmentService.findDeptListToAdd();
			request.setAttribute("findDeptList", findDeptList);
			return "toAddUser";
		} catch (Exception e) {
			return e.getMessage();
		}
		
	}
	
	/**
	 * 添加用户
	 * @param user
	 * @param session
	 * @return
	 */
	@RequestMapping("/addUser")
	public String insertUser(TbUser user,HttpServletRequest request, HttpSession session){
		try {
			 userService.addUser(user,request,session);
			return "redirect:/user/findUserList?pageNum=1";
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return e.getMessage();
		}
	}
	
	/**
	 * 查询所有用户
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping("/findUserList")
	public String findUserlist(TbUser user,Integer pageNum,HttpServletRequest request){
		try {
			PageInfo<TbUser> findUserList = userService.findUserList(pageNum);
			findUserList.getTotal();
			request.setAttribute("page", findUserList);
			request.setAttribute("findUserList", findUserList.getList());
			return "userList";
		} catch (Exception e) {
			return e.getMessage();
		}
	}
	
	/*@RequestMapping("/toUpdateUser")
	public String toUpdateUser(){
		return "toUpdateUser";
	}*/
	
	/**
	 * 模糊查询
	 * @param userName
	 * @param request
	 * @return
	 */
	@RequestMapping("/selectUserByName")
	public String selectUserByName(String userName,HttpServletRequest request,Integer pageNum){
		try {
			PageInfo<TbUser> page=userService.selectUserByName(userName,pageNum);
			request.setAttribute("page",page);
			request.setAttribute("findUserList", page.getList());
			return "userList";
		} catch (Exception e) {
			return e.getMessage();
		}
	}
	
	/**
	 * 更新用户
	 * @param user
	 * @param session
	 * @return
	 */
	@RequestMapping("/updateUser")
	public String updateUser(TbUser user,HttpSession session){
		try {
			userService.updateUser(user,session);
			return "redirect:/user/findUserList?pageNum=1";
		} catch (Exception e) {
			return e.getMessage();
		}
	}
	
	/**
	 * 逻辑删除用户
	 * @param user
	 * @return
	 */
	@RequestMapping("/deleteUser")
	public String deleteUser(TbUser user){
		try {
			userService.deleteUser(user);
			return "redirect:/user/findUserList?pageNum=1";
		} catch (Exception e) {
			return e.getMessage();
		}
	}
	
	/**
	 * 为更新查找用户
	 * @param userId
	 * @param request
	 * @return
	 */
	@RequestMapping("/selectUserById")
	public String selectUserById(String userId,HttpServletRequest request){
		try {
			TbUser user = userService.selectUserById(userId);
			request.setAttribute("user1", user);
			List<TbDepartment> findDeptList=departmentService.findDeptListToAdd();
			request.setAttribute("findDeptList",findDeptList);
			return "toUpdateUser";
		} catch (Exception e) {
			return e.getMessage();
		}
	}
	
	@RequestMapping("/homepage")
	public String homepage(){
		return "homePage";
	}
	
	/**
	 * 跳转至修改密码页面
	 * @return
	 */
	@RequestMapping("/toUpdatePass")
	public String toUpdatePass(){
		return "updatePassword";
	}
	
	
	@RequestMapping("/updatePass")
	public String updatePass(String userId,String oldPassword, String newPassword,String confirmPass){
		try {
			userService.updatePass(userId,oldPassword,newPassword,confirmPass);
			return "passAfter";
		} catch (Exception e) {
			return e.getMessage();
		}
	}

	@RequestMapping("/logout")
	public String logout(HttpSession session){
		session.removeAttribute("user");
		return "login";
		
	}
	
	/**
	 * 设为管理员
	 * @param userId
	 * @return
	 */
	@RequestMapping("/addAdmin")
	public String addAdmin(TbUser user){
		userService.addAdmin(user);
		return "redirect:/user/findUserList?pageNum=1";
	}
}
